package com.BCM.controller;


import com.BCM.po.CClass;
import com.BCM.po.User;

import com.BCM.service.CClassService;
import com.BCM.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author tanghao
 */
@Controller("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CClassService cClassService;
    @RequestMapping(value = "/UserList.action")
    public String UserList(Model model){
        User user=new User();
        List<User>users=userService.findAllUser();

        model.addAttribute("Users",users);
        return "UserList";
    }
    @RequestMapping(value = "/deleteUser.action")
    @ResponseBody
    public String deleteUser(int id){
        int num=userService.deleteUser(id);
        if(num>0){
            return "OK";
        }else{
            return "FALL";
        }
    }

    @RequestMapping("getUserById.action")
    @ResponseBody
    public User getUserById(int id){
        System.out.println("进入getUser");
        User user=this.userService.findUserById(id);
        System.out.println(user);
        return user;
    }
    @RequestMapping("/updateUser.action")
    @ResponseBody
    public String updateUser(User user){
        int num=this.userService.updateUser(user);
        if(num>0){
            return "OK";
        }else {
            return "FALL";
        }
    }
    @RequestMapping("/toAddUser")
    public String toAddCustomer(Model model){
        User user=new User();
        List<User>users=userService.findAllUser();

        model.addAttribute("Users",users);
        CClass cClass=new CClass();
        List<CClass>cClasses=cClassService.findAllCClass();
        model.addAttribute("cClasses",cClasses);
        return "/UserAdd";
    }

    @RequestMapping("/toAddGrade")
    public String toAddGrade(Model model){
        User user=new User();
        List<User>users=userService.findAllUser();
        CClass cClass=new CClass();
        List<CClass>cClasses=cClassService.findAllCClass();
        model.addAttribute("cClasses",cClasses);
        model.addAttribute("Users",users);

        return "/GradeAdd";
    }



    @RequestMapping("/addUser")
    public String addUser(User user,Model model){

        userService.save(user);
        return "redirect:/UserList.action";
    }
    @RequestMapping("/findUserBystudentClass")
    public String findUserBystudentClass(String studentClass,Model model){

        List<User> users=this.userService.findUserBystudentClass(studentClass);
        for (User user:users){
            System.out.println("controller"+user);
        }
        model.addAttribute("Users",users);
        return "UserList";
    }




    @RequestMapping("/student_index")
    public String tostudent_index(){
        return "/student_index";
    }

}
